extends Node2D
@export var host:Node2D
var _track:bool=false
var a:int=0
var init_pos:Vector2
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	$Display.material=$Display.material.duplicate()
	$Display.material.get_shader_parameter("noise").noise.seed=randi()
	$Area2D.set_deferred("monitoring",false)
	init_pos=self.global_position
var twn:Tween
func shoot()->void:
	init_pos=self.global_position
	$WaterBubbles.play()
	twn=create_tween()
	twn.tween_property($Display.material,"shader_parameter/alpha",0.3,0.8)
	twn.tween_callback(func():
		_track=true
		$Area2D.set_deferred("monitoring",true)
		)
	twn.tween_property(self,"a",0,10)
	twn.tween_callback(func():
		_track=false
		$Area2D.set_deferred("monitoring",false)
		)
	twn.tween_property($Display.material,"shader_parameter/alpha",0.0,1.0)
func reset()->void:
	_track=false
	$Area2D.set_deferred("monitoring",false)
	if twn:if twn.is_running():twn.kill()
	twn=create_tween()
	twn.tween_property($Display.material,"shader_parameter/alpha",0.0,0.5)
	twn.tween_callback(func():
		self.global_position=init_pos
		)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	if _track:
		self.global_position=self.global_position.move_toward(Global.player_position,delta*400)

func _on_area_2d_area_entered(area: Area2D) -> void:
	if not area.is_in_group("player"):return
	_track=false
	$WaterBubbles.play()
	$Area2D.set_deferred("monitoring",false)
	Global.camera_effect.emit("spring_shake")
	if twn.is_running():twn.kill()
	twn=create_tween()
	twn.tween_property($Display.material,"shader_parameter/alpha",1.0,0.5)
	twn.tween_property($Display.material,"shader_parameter/alpha",0.0,0.5)
	await twn.finished
	if host:host.close_waterball()
	Global.player_movment_mode.emit("tie")
	Global.player_should_death.emit()
